package com.youth.api;

import com.youth.aop.OperationLogAnnotation;
import com.youth.entity.PageBean;
import com.youth.entity.vo.CommodityVO;
import com.youth.log.OperationType;
import com.youth.result.ResultBean;
import com.youth.service.CommodityService;
import com.youth.service.vo.ICommodityVOService;
import com.youth.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @ClassName CommodityApi
 * @Description: TODO 商品控制层
 * @Author yjwu
 * @Date 2019/11/13 23:26
 **/
@RestController
@Api(value = "商品，控制层")
@Slf4j
@RequestMapping("/commodity")
public class CommodityApi implements ICommodityVOService {
    @Autowired
    private CommodityService commodityService;


    @Override
    @OperationLogAnnotation(operationType = OperationType.ADD, operationModual = "商品", operationDesc = "商品模块添加接口")
    @ApiOperation(value = "商品添加接口", notes = "商品添加接口", response = ResultBean.class, produces = "application/json")
    public ResultBean add(@RequestBody @Valid CommodityVO commodityVO) {
        try {
            commodityService.add(commodityVO);
            return ResultBean.returnInsertSuccess(commodityVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.returnInsertUnSuccess(null);
        }
    }

    @Override
    @ApiOperation(value = "添加接口", notes = "excel文件导入数据")
    public ResultBean addByExcel(@RequestParam("excel") MultipartFile excel) {
        try {
            return ResultBean.returnInsertSuccess(commodityService.addByExcel(excel));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.returnInsertUnSuccess(null);
        }
    }

    @Override
    @ApiOperation(value = "删除接口", notes = "模块使用")
    public ResultBean delete(@RequestParam("code") String code) {
        try {
            Integer delete = commodityService.delete(code);
            return ResultBean.returnDeleteSuccess(delete);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.returnDeleteUnSuccess(null);
        }
    }

    @Override
    @ApiOperation(value = "修改接口", notes = "模块使用")
    public ResultBean edit(CommodityVO commodityVO) {
        try {
            Integer edit = commodityService.edit(commodityVO);
            return ResultBean.returnUpdateSuccess(edit);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.returnUpdateSuccess(null);
        }
    }

    @Override
    @ApiOperation(value = "减少库存接口", notes = "模块使用")
    public ResultBean decreaseStockNumber(@RequestParam("code") String code,
                                          @RequestParam("soldNumber") Integer soldNumber) {
        try {
            if (StringUtils.isNotEmpty(code) && soldNumber != null) {
                return ResultBean.returnUpdateSuccess(commodityService.decreaseStockNumber(code, soldNumber));
            }
            return ResultBean.returnUpdateUnSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.returnUpdateUnSuccess(null);
        }
    }

    @Override
    @ApiOperation(value = "查询接口", notes = "根据code查询")
    public ResultBean findByCode(@RequestParam("code") String code) {
//        try {
//            Subject subject = SecurityUtils.getSubject();
//            if (null != subject) {
//                User user = (User) subject.getPrincipal();
//                log.info(user.getUsername());
//            }
//            return ResultBean.returnSelectSuccess(commodityService.findByCode(code));
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultBean.returnSelectUnSuccess(null);
//        }
        return null;
    }

    @Override
    @ApiOperation(value = "查询接口", notes = "条件查询")
    public ResultBean findWithConditions(@RequestBody PageBean pageBean) {
        return null;
    }
}
